/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package central;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SchabauerJ
 */
public class UDP_Client {

    private Logger logger = null;
    private byte[] sendData = new byte[1024];
    private byte[] receiveData = new byte[1024];
    private DatagramSocket clientSocket = null;
    private InetAddress IPAddress = null;
    private int IPPort= 8888;  //default
    public int[] ADC_data = new int[16];

    /*
     * Contstructor
     */
    UDP_Client() {
        logger = Logger.getLogger(UDP_Client.class.getName());
        //Create 2 threads for decoupeling sending and receiving data
    }

    public void connect(String ip_addr, int port) {
        //BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        try {
            clientSocket = new DatagramSocket();
            IPAddress = InetAddress.getByName(ip_addr);
            IPPort= port;

        } catch (SocketException ex) {
            Logger.getLogger(UDP_Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnknownHostException ex) {
            Logger.getLogger(UDP_Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //String sentence = inFromUser.readLine();
    public void sendCMD(String cmd) {
        try {
            sendData = cmd.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, IPPort);
            clientSocket.send(sendPacket);

             DatagramPacket receivePacket = new DatagramPacket(receiveData,
             receiveData.length); clientSocket.receive(receivePacket); String
             modifiedSentence = new String(receivePacket.getData());
             System.out.println("FROM SERVER:" + modifiedSentence);

        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }

    }

    public void disconnect() {
        clientSocket.close();
    }

    public void clear_Datagram(String cmd) {
        DatagramSocket clientSocket = null;
    }
}
